package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.iwith.dongmoon.gsonlist.MemberList;
import com.iwith.dongmoon.gsonlist.MemberListItem;
import com.iwith.dongmoon.listadpater.FiveTextListAdapter2;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_InterestsActivity extends Activity implements OnTabChangeListener {
	
	private ListView followingListView;
	private ListView followerListView;

	private RequestGson requestGson = new RequestGson();
	
	private TabHost mTabHost;
	
	private String [] TAB_ID = { ApiCode.FOLLOWING, ApiCode.FOLLOWER };
	private String [] TAB_NAME = { "내가 관심있는 동문", "나에게 관심있는 동문" };
	private Integer [] TAB_RESOURCE = { R.id.tabinterests_myfavoritelistview, R.id.tabinterests_favoritemelistview };
	
	private String userid;
	private SessionManager mSessionManager=null;
	
	private Context mContext = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_interests);
		mSessionManager=new SessionManager(this);
		
		// TODO Auto-generated method stub
		mTabHost = (TabHost)findViewById(R.id.tabinterests_tabhost);
		mTabHost.setup();
		
		for (int i=0; i<TAB_ID.length; i++){
			mTabHost.addTab(mTabHost.newTabSpec(TAB_ID[i]).setIndicator(TAB_NAME[i]).setContent(TAB_RESOURCE[i]));
			mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 70;
		}
	}

	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}

	private void Initialize() {
		mContext = this;
		userid = mSessionManager.getUserid();
				
		followingListView = (ListView) findViewById(R.id.tabinterests_myfavoritelistview);
		followerListView = (ListView) findViewById(R.id.tabinterests_favoritemelistview);
		
		if(mTabHost.getCurrentTab()==0){
			setMemberListData(ApiCode.FOLLOWING, followingListView);
		} else if(mTabHost.getCurrentTab()==1){
			setMemberListData(ApiCode.FOLLOWER, followerListView);
		}
		mTabHost.setOnTabChangedListener(this);
	}

	private void setMemberListData(final String code, final ListView listview){
		ThreadInterface threadInterface = new ThreadInterface() {
			MemberList memberList=null;
			public void onThread() {
				String api=null;
				if(code.equals(ApiCode.FOLLOWING)==true){
					api=ApiCode.API_FOLLOWING;
				} else if (code.equals(ApiCode.FOLLOWER)==true){
					api=ApiCode.API_FOLLOWER;
				}
				memberList = requestGson.getMemberGsonBySendingAPI(mContext, api, "GET", "/"+userid);
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub
				if (memberList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					ListAdapter listAdapter = new FiveTextListAdapter2(mContext, R.layout.listitem_fivetext2, (ArrayList<MemberListItem>) memberList.getList());
					listview.setAdapter(listAdapter);
				} else {
					requestGson.alertToast(mContext, code+" 데이터 로딩 실패");
				}
			}
		};
		
		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}

	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		if(tabId.equals(TAB_ID[0])==true){
			setMemberListData(ApiCode.FOLLOWING, followingListView);
		} else if(tabId.equals(TAB_ID[1])==true){
			setMemberListData(ApiCode.FOLLOWER, followerListView);
		}
	}
}